/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.service.services.references;

import java.util.StringTokenizer;


/**
 * This class is used to define an element of references list such as defined
 *  in the references_conf.xml.
 *
 */
public class ReferenceElement implements Comparable
{
    /** Separator. */
    public static final String SEPARATOR = "=";

    /** Label of the attribute. */
    private String label;

    /** Value of the attribute. */
    private String value;

    /**
     * Creates a new ReferenceElement object.
     *
     * @param newlabel   Label or value=label if the <i>isToParse</i> argument equals true.
     * @param isToParse  Indicates if the first argument must be parsed.
     */
    public ReferenceElement(String newlabel, boolean isToParse)
    {
        if (isToParse)
        {
            // using the separator, if only one element found
            // use it as label and value !
            StringTokenizer tok = new StringTokenizer(newlabel, SEPARATOR);

            if (tok.hasMoreTokens())
            {
                this.value = tok.nextToken().trim();
            }

            if (tok.hasMoreTokens())
            {
                this.label = tok.nextToken().trim();
            }
            else
            {
                this.label = this.value;
            }
        }
        else
        {
            this.label = newlabel;
            this.value = newlabel;
        }
    }

    /**
     * Creates a new ReferenceElement object.
     *
     * @param newlabel  Label of attribute.
     * @param newvalue  Value of the attribute.
     */
    public ReferenceElement(String newlabel, String newvalue)
    {
        this.label = newlabel;
        this.value = newvalue;
    }

    /**
     * Gets the label of the attribute.
     *
     * @return The label.
     */
    public String getLabel()
    {
        return label;
    }

    /**
     * Gets the value of the attribute.
     *
     * @return The value.
     */
    public String getValue()
    {
        return value;
    }

    /**
     * Sets the label of the attribute.
     *
     * @param alabel The label to set
     */
    public void setLabel(String alabel)
    {
        this.label = alabel;
    }

    /**
     * Sets the value of the attribute.
     *
     * @param avalue The value to set
     */
    public void setValue(String avalue)
    {
        this.value = avalue;
    }

    /**
     * Converts the reference element into a String.
     *
     * @return The String representing the reference element.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("label = ");
        buf.append(label);
        buf.append(System.getProperty("line.separator"));
        buf.append("value = ");
        buf.append(value);
        buf.append(System.getProperty("line.separator"));

        return buf.toString();
    }

    // end of toString method

    /**
     * Compares the current label with the label of the ReferenceElement object
     *  given in argument.
     *
     * @param arg0 object to be compared with
     *
     * @return the value 0 if the labels are equals;
     * a value less than 0 if the label in argument is a string
     * lexicographically greater than the current label;
     *  and a value greater than 0 if the label in argument is a string
     * lexicographically less than the current label.
     *
     * @see java.lang.Comparable#compareTo(Object)
     */
    @Override
	public int compareTo(Object arg0)
    {
        ReferenceElement elt = (ReferenceElement) arg0;

        return label.compareTo(elt.getLabel());
    }
}
